﻿using System;
using System.Data;
using System.Collections;
using System.Data.SqlClient;

using DeviceInfoManage.DataAccessLayer;
using DeviceInfoManage.DataAccessHelper;
using System.Text;


/// <summary>
/// Admin 的摘要说明
/// </summary>
/// 
namespace DeviceInfoManage.BusinessLogicLayer
{
    /*本业务类主要处理于管理员帐号信息相关的功能*/
    public class Admin
    {
        private string adminUserName; //登陆用户名
        private string adminPassword; //登陆密码,已经经过md5哈希加密
        private string userName;      //用户名
        private string adminType;     //用户类型
        private string errMessage;    //登陆失败原因

        #region MyRegion
        public string AdminUserName
        {
            set
            {
                this.adminUserName = value;
            }
            get
            {
                return this.adminUserName;
            }
        }

        public string AdminPassword
        {
            set
            {
                this.adminPassword = value;
            }
            get
            {
                return this.adminPassword;
            }
        }
        public string AdminType
        {
            set { this.adminType = value; }
            get { return this.adminType; }
        }
        public string ErrMessage
        {
            set
            {
                this.errMessage = value;
            }
            get
            {
                return this.errMessage;
            }
        }
        public string UserName
        {
            set { userName = value; }
            get { return userName; }
        }
        #endregion


        //判断用户的登陆管理权限
        public bool checkAdmin()
        {
            string queryString;
            bool hasUser, isPasswordRight;

            //首先在数据库中查询该管理帐号是否存在
            queryString = "select * from admin where adminUserName = " + SqlString.GetQuotedString(this.adminUserName);
            DataBase db = new DataBase();
            hasUser = db.GetRecord(queryString);
            if (false == hasUser)
            {
                errMessage = "对不起，用户名不存在!";
                return false;
            }

            //再查询数据库该管理帐号的密码是否正确
            queryString = "select * from admin where adminUserName = " + SqlString.GetQuotedString(this.adminUserName);
            queryString = queryString + " and adminPassword = " + SqlString.GetQuotedString(this.adminPassword);
            isPasswordRight = db.GetRecord(queryString);
            if (false == isPasswordRight)
            {
                errMessage = "对不起，用户密码错误!";
                return false;
            }
            DataSet ds = db.GetDataSet(queryString);
            //adminUserName = ds.Tables[0].Rows[0]["adminUserName"].ToString();
            AdminType = ds.Tables[0].Rows[0]["adminType"].ToString();
            UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
            return true;
        }

        //修改登陆密码
        public bool ChangePassword()
        {
            string updateString = "update admin set adminPassword=" + SqlString.GetQuotedString(adminPassword);
            updateString += " where adminUserName=" + SqlString.GetQuotedString(adminUserName);

            DataBase db = new DataBase();
            if (db.InsertOrUpdate(updateString) < 0)
                return false;
            return true;
        }

        public DataSet GetUsers()
        {
            string queryString = "select *  from admin";
            return DbHelperSQL.Query(queryString);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Admin model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into admin(");
            strSql.Append("adminUserName,adminPassword,UserName,adminType)");
            strSql.Append(" values (");
            strSql.Append("@adminUserName,@adminPassword,@UserName,@adminType)");
            SqlParameter[] parameters = {
					new SqlParameter("@adminUserName", SqlDbType.VarChar,20),
					new SqlParameter("@adminPassword", SqlDbType.VarChar,32),
                    new SqlParameter("@UserName",SqlDbType.VarChar,50),
					new SqlParameter("@adminType", SqlDbType.VarChar,20)};
            parameters[0].Value = model.adminUserName;
            parameters[1].Value = model.adminPassword;
            parameters[2].Value = model.UserName;
            parameters[3].Value = model.adminType;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Admin model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update admin set ");
            strSql.Append("adminPassword=@adminPassword,");
            strSql.Append("UserName=@UserName,");
            strSql.Append("adminType=@adminType");
            strSql.Append(" where adminUserName=@adminUserName ");
            SqlParameter[] parameters = {
					new SqlParameter("@adminUserName", SqlDbType.VarChar,20),
					new SqlParameter("@adminPassword", SqlDbType.VarChar,32),
                    new SqlParameter("@UserName",SqlDbType.VarChar,50),
					new SqlParameter("@adminType", SqlDbType.VarChar,20)};
            parameters[0].Value = model.adminUserName;
            parameters[1].Value = model.adminPassword;
            parameters[2].Value=model.UserName;
            parameters[3].Value = model.adminType;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string adminUserName)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from admin ");
            strSql.Append(" where adminUserName=@adminUserName ");
            SqlParameter[] parameters = {
					new SqlParameter("@adminUserName", SqlDbType.VarChar,50)};
            parameters[0].Value = adminUserName;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
    }
}

